import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/ant-design-vue.js'
import { generateRouter } from "./utils/RouterTree";


//定义变量判断是否已经动态添加过，如果刷新后load永远为 0
let load = 0

router.beforeEach(async (to, from, next) => {
  let along = JSON.parse(sessionStorage.getItem("along"));
  let newRouters = "";
  if (along.root != undefined) {
    // 后台获取的路由表格式化成路由形式
    newRouters = generateRouter(along.root.userRouters);
  }

  //非登录、有菜单数据、 没有进行添加（或者刷新了）
  if (load === 0 && newRouters != "" && to.name !== '/') {
    load++;
    // 动态添加路由
    router.addRoutes(newRouters);
    // 更改权限状态
    store.dispatch("setAddRouters")
    next({ path: to.path })
  }
  else {
    next();
  }
})


Vue.config.productionTip = false
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
